package com.bicon.botu.account.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.account.entity.Resources;
import com.bicon.botu.account.service.IResourcesService;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 資源增删改查接口类
 *
 * @author peizp
 * @date 2017/6/14 0012 下午 2:47
 */
@Slf4j
@RestController
@RequestMapping("/V1.0.0/resource")
public class ResourceController extends BaseController {

    @Autowired
    private IResourcesService iResourcesService;

    /**
     * 新增资源
     *
     * @param name     资源名
     * @param parentId 资源父ID
     * @param sort     排序
     * @param path     资源路径
     * @param type     账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam String name, String parentId, @RequestParam String sort, String path, @RequestParam String type) {
        return iResourcesService.add(name, parentId, sort, path, type);
    }

    /**
     * 删除资源
     *
     * @param idJson 资源id集合，多个使用","分隔
     * @param type   账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/delete")
    public BaseResult delete(@RequestParam String idJson, String type) {
        return iResourcesService.delete(idJson, type);
    }

    /**
     * 修改资源
     *
     * @param id   资源id
     * @param path 资源路径
     * @param name 资源名
     * @param type 账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam String id, @RequestParam String path, @RequestParam String name, @RequestParam String type) {
        return iResourcesService.update(id, path, name, type);
    }

    /**
     * 查询树形资源
     *
     * @param type 账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/selectAll")
    public BaseResult selectAll(@RequestParam String type) {
        return iResourcesService.selectResourcesAll(type);
    }

    /**
     * 分页获取资源列表
     *
     * @param type    账户类型：0：前台账户，1：后台账户
     * @param current 当前页
     * @param size    每页记录数
     * @return
     */
    @RequestMapping(value = "/getPagingResources")
    public BaseResult getPagingResources(@RequestParam String type, @RequestParam int current, @RequestParam int size) {
        Page<Resources> resourcesPage = iResourcesService.getPagingResources(type, current, size);
        return sendResult200(resourcesPage);
    }

}
